<?php

namespace Console\Controller;

/**
 * 用户控制器
 */
class UserController extends AuthController {

    private $authLogic;

    public function _initialize() {
        parent::_initialize();
        $this->authLogic = A('Console/Auth', 'Logic');
    }

    /**
     * 用户列表
     */
    public function lists() {
        $this->display();
    }

    /**
     * 获取用户列表
     */
    public function doGetUserList() {
        $where = I("post.");

        foreach ($where as $key => $val) {
            if (empty($val)) {
                unset($where[$key]);
            }
        }

        $list = $this->authLogic->getUserList($where);
        if (empty($list)) {
            $this->ajaxReturn(static::$res->err("暂无数据"));
        }

        $this->assign("list", $list);
        $html = $this->fetch("doGetUserList");
        $resData = array(
            "html" => $html
        );
        $this->ajaxReturn(static::$res->suc("成功", $resData));
    }

    /**
     * 获取添加用户表单
     */
    public function doGetAddUserForm() {
        $html = $this->fetch("doGetAddUserForm");
        $resData = array(
            "html" => $html
        );
        $this->ajaxReturn(static::$res->suc("成功", $resData));
    }

    /**
     * 获取编辑用户列表
     */
    public function doGetEditUserForm() {
        $id = I('post.id');
        if (empty($id)) {
            $this->ajaxReturn(static::$res->err("编辑ID不能为空"));
        }

        $where = array(
            "id" => $id
        );
        $info = $this->authLogic->find($where);

        $this->assign("info", $info);
        $html = $this->fetch("doGetEditUserForm");
        $resData = array(
            "html" => $html
        );
        $this->ajaxReturn(static::$res->suc("成功", $resData));
    }

    /**
     * 添加用户
     */
    public function doAddUser() {
        $data = I('post.');

        $userInfo = $this->getUserInfo();
        $userId = $userInfo["id"];

        $res = $this->authLogic->addUser($userId, $data);
        $this->ajaxReturn($res);
    }

    /**
     * 编辑用户
     */
    public function doEditUser() {
        $userInfo = $this->getUserInfo();
        $userId = $userInfo["id"];

        $data = I('post.');
        $id = $data["id"];
        unset($data["id"]);

        $res = $this->authLogic->editUser($userId, $id, $data);
        $this->ajaxReturn($res);
    }

    /**
     * 删除用户
     */
    public function doDelUser() {
        $userInfo = $this->getUserInfo();
        $userId = $userInfo["id"];

        $id = I('post.id');

        $res = $this->authLogic->delUser($userId, $id);
        $this->ajaxReturn($res);
    }

}
